//
//  LXRecommendViewController.swift
//  zhibo
//
//  Created by Echo on 16/9/22.
//  Copyright © 2016年 Echo. All rights reserved.
//

import UIKit

private let kItemMargin : CGFloat = 10
private let kItemW = (kScreenW - 3 * kItemMargin) * 0.5
private let kNormalItemH = kItemW * 3 / 4
private let kPrettyItemH = kItemW * 4 / 3
private let kHeaderViewH : CGFloat = 50

private let kCircleViewH = kScreenW * 3 / 8
private let kGameViewH : CGFloat = 90


private let kNormalCellId = "kNormalCellId"
private let kPrettyCellId = "kPrettyCellId"
private let kHeaderViewId = "kHeaderViewId"

class LXRecommendViewController: UIViewController {
    
    // MARK:- 属性配置
    /// VM
    private lazy var viewModel : LXRecommendViewModel = LXRecommendViewModel()
    /// 主体collectionView
    private lazy var collectionView : UICollectionView = {[unowned self] in
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSizeMake(kItemW, kNormalItemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = kItemMargin
        layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
        layout.sectionInset = UIEdgeInsets(top: 0, left: kItemMargin, bottom: 0, right: kItemMargin)
        
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.whiteColor()
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.autoresizingMask = [.FlexibleWidth,.FlexibleHeight]
        collectionView.registerNib(UINib(nibName:"LXNormalCell",bundle: nil), forCellWithReuseIdentifier: kNormalCellId)
        collectionView.registerNib(UINib(nibName:"LXPrettyCell",bundle: nil), forCellWithReuseIdentifier: kPrettyCellId)
       collectionView.registerNib(UINib(nibName:"LXHeaderReusableView",bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderViewId)
        
        return collectionView
    }()
    
    private lazy var circleView : LXRecommendCircleView = {
        let circleView = LXRecommendCircleView.recommendCircleView()
        circleView.frame = CGRect(x: 0, y: -(kCircleViewH + kGameViewH), width: kScreenW, height: kCircleViewH)
        return circleView
    }()
    
    private lazy var gameView : LXRecommendGameView = {
        let gameView = LXRecommendGameView.recommendGameView()
        gameView.frame = CGRect(x: 0, y: -(kGameViewH), width: kScreenW, height: kGameViewH)
        return gameView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        view.backgroundColor = UIColor.whiteColor()
        
        setupUI()
        
        loadData()

    }
}

extension LXRecommendViewController {
    private func setupUI() {
        
        view.addSubview(collectionView)
        
        collectionView.addSubview(circleView)
        
        collectionView.addSubview(gameView)
        
        collectionView.contentInset = UIEdgeInsets(top: kCircleViewH + kGameViewH, left: 0, bottom: 0, right: 0)
        
    }
    
    private func loadData() {
        viewModel.requestData { 
            self.collectionView.reloadData()
            
            self.gameView.groups = self.viewModel.anchorGroups
        }
        
        viewModel.requestCircleData { 
            self.circleView.circleModels = self.viewModel.circleModels
        }
        
    }
}

extension LXRecommendViewController : UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return viewModel.anchorGroups.count
        
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        if section == 1 {
//            return 8
//        }
//        return 4
        let group = viewModel.anchorGroups[section]
        return group.anchors.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let group = viewModel.anchorGroups[indexPath.section]
        let anchor = group.anchors[indexPath.item]
        
        var cell : LXBaseCell!
        if indexPath.section == 1 {
            cell = collectionView.dequeueReusableCellWithReuseIdentifier(kPrettyCellId, forIndexPath: indexPath) as! LXPrettyCell
            
        }else {
            cell = collectionView.dequeueReusableCellWithReuseIdentifier(kNormalCellId, forIndexPath: indexPath) as!
            LXNormalCell
            
        }
        
        cell.anchor = anchor
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: kHeaderViewId, forIndexPath: indexPath) as! LXHeaderReusableView
        
        headerView.group = viewModel.anchorGroups[indexPath.section]
         
        
        return headerView
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        if indexPath.section == 1 {
            return CGSize(width: kItemW, height: kPrettyItemH)
        }
        
        return CGSize(width: kItemW, height: kNormalItemH)
        
    }
}
